/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var googlemap = null;
var heatmap = null;
var filterOnibus = [];

function getBusInformation() {
    
    $("#HeatLoading").css("display", "inline");
    
    var ulrGPSBusao = "http://dadosabertos.rio.rj.gov.br/apiTransporte/apresentacao/rest/index.cfm/obterTodasPosicoes";

    $.ajax({
        url: ulrGPSBusao,
        beforeSend: function () {

        },
        success: function (result) {
            $("#HeatLoading").css("display", "none");
            fillBusInformation(result);
        },
        error: function (result) {
            $("#HeatLoading").css("display", "none");
            alert("erro: " + result);
        }
    });
}

function reloadBusInformation() {
    if($('#heatmap-checkbox').checked) {
        getBusInformation();
    }
    else
    {
        if(heatmap === null) {
            $('#heatmap-checkbox').prop('checked', true);
        }
        getBusInformation();
    }
}

function getBusList(onibustext) {
    
    var ulrBusaoList = "./onibus.htm";
    
    $.ajax({
        url: ulrBusaoList,
        beforeSend: function () {

        },
        success: function (result) {
            onibustext.html(result);
        },
        error: function (result) {
            alert("erro: " + result);
        }
    });
}

function filterBusInformation(filtro) {
    if(filtro.checked) {
        filterOnibus.push(filtro.id);
    }
    else {
        var found = $.inArray(filtro.id, filterOnibus);
        if(found > -1) {
           filterOnibus.splice(found, 1);
        }
    }
}

function fillBusInformation(resultado) {

    var taxiData = [];

    for (i = 0; i < resultado["DATA"].length; i++) {
        var linha = resultado["DATA"][i][2].toString();
        
        var found = filterOnibus.length === 0 || $.inArray(linha, filterOnibus) > -1;
        if(found)
            taxiData[i] = new google.maps.LatLng(resultado["DATA"][i][3], resultado["DATA"][i][4]);
    }

    var pointArray = new google.maps.MVCArray(taxiData);

    if (heatmap !== null)
        heatmap.setMap(null);

    heatmap = new google.maps.visualization.HeatmapLayer({
        data: pointArray
    });

    heatmap.set('maxIntensity', 1);
    
    heatmap.setMap(googlemap);
}
